package com.shanghaionstar.nav;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.model.LatLng;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.iview.INavChoosePointInMapView;
import com.shanghaionstar.nav.model.MapChoosePoiType;
import com.shanghaionstar.nav.presenter.NavChoosePointInMapPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.sqlite.UserAddressInfoImpl;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.MapHandler;
import com.shanghaionstar.utils.MyLocationUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 6335yz on 2016/7/18.
 */
public class NavChoosePointInMapFragment extends BaseFragment implements INavChoosePointInMapView {

    @FindViewById(R.id.mapview)
    private OMapView mapview;
    // 选点成功过后的view
    @FindViewById(R.id.map_choosing_point_info_view)
    private RelativeLayout map_choosing_point_info_view;
    // 商家名称
    @FindViewById(R.id.text_map_choosing_point_name)
    private TextView text_map_choosing_point_name;
    // 商家地址
    @FindViewById(R.id.text_vendor_addres)
    private TextView text_vendor_addres;
    // 确定 返回到设置家的地址
    @FindViewById(R.id.btn_sethome_address)
    private RippleCustomBtn btn_sethome_address;
    private boolean isLocationing = false;
    private MyLocationUtil myLoationUtil = null;
    //用来标记是设置家或是公司的地址
    private UserAddressInfoImpl userAddressInfoImpl;
    private String flag;
    private int type;
    private int startorendlocation;//标记路线规划的起始和终点位置

    public static final int SEARCHMAIN = 0;
    public static final int ROUTEPLAN = 1;

    public static final int STARTLOCATION = 2;
    public static final int ENDLOCATION = 3;
    private MapHandler mapHandler;
    private AbstractMapActivity mapActivity;
    private FragmentManager fragmentManager;
    private AMap aMap;
    private NavChoosePointInMapPresenter navChoosePointInMapPresenter;
    private PoiItemInfo markerPoi;
    private MapChoosePoiType mapChoosePoiType;
    @FindViewById
    private TopLayout top_layout;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            mapview.onCreate(savedInstanceState);
            aMap = mapview.getMap();
            mapHandler = new MapHandler(mapActivity, mapview.getMap());
            mapActivity.initChooseMapHandler(mapHandler);
            navChoosePointInMapPresenter = new NavChoosePointInMapPresenter(mapActivity.getScheduler(), mapActivity, mapHandler);
            navChoosePointInMapPresenter.setView(this);
            aMap.setOnMapLongClickListener(new AMap.OnMapLongClickListener() {
                @Override
                public void onMapLongClick(LatLng arg0) {
                    PoiItemInfo poiItemInfo = new PoiItemInfo();
                    poiItemInfo.setLatitude(arg0.latitude);
                    poiItemInfo.setLongitude(arg0.longitude);
                    navChoosePointInMapPresenter.showMapPoint(poiItemInfo);
                }
            });
            if (getArguments() != null) {
                if (getArguments().getSerializable("mapChoosePoiType") != null) {
                    mapChoosePoiType = (MapChoosePoiType) getArguments().getSerializable("mapChoosePoiType");
                }
            }
            top_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Mappoint_return.getStringValue());
                    goBack();
                }
            });
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mapActivity = (AbstractMapActivity) activity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    @Override
    public int getLayoutId() {
        return R.layout.nav_map_choosing_point;
    }

    /**
     * @param @param poiItemInfo
     * @param @param locationType
     * @Title: showMarkerInfoView
     * @Description: 显示marker对应的信息
     */
    @Override
    public void showMarkerInfoView(PoiItemInfo poiItemInfo, LocationType locationType) {
        if (poiItemInfo != null) {
            markerPoi = poiItemInfo;
            map_choosing_point_info_view.setVisibility(View.VISIBLE);
            if (!CommonUtil.isNull(poiItemInfo.getTitle())) {
                text_map_choosing_point_name.setText(poiItemInfo.getTitle());
            } else {
                text_map_choosing_point_name.setText("----");
            }

            if (!CommonUtil.isNull(poiItemInfo.getAddress())) {
                text_vendor_addres.setText(poiItemInfo.getAddress());
            } else {
                text_vendor_addres.setText("");
            }
        }
    }

    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {
        ToastUtil.show(mapActivity, e.getMessage());
    }

    @Override
    public void onReponseSuccess(PoiItemInfo poiItemInfo) {
        mapActivity.removeUntilFragment(fragmentManager, getPreFragment(), false);

//        if(getPreFragment() == null) {
//            Intent intent = new Intent();
//            SetAddressResult setAddressResult = new SetAddressResult();
//            intent.putExtra("result",poiItemInfo);
//            mapActivity.setResult(1,intent);
//            mapActivity.finish();
//        }
    }

    @Override
    public Activity onActivity() {
        return null;
    }

    @OnClick({R.id.btn_sethome_address})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sethome_address:
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Mappoint_confirm.getStringValue());
                navChoosePointInMapPresenter.submit(String.valueOf(text_map_choosing_point_name.getText()), String.valueOf(text_vendor_addres.getText()), markerPoi, mapChoosePoiType);
                break;
            default:
                break;
        }
    }

    public static NavChoosePointInMapFragment newInstance(MapChoosePoiType mapChoosePoiType) {
        Bundle bundle = new Bundle();
        NavChoosePointInMapFragment navChoosePointInMapFragment = new NavChoosePointInMapFragment();
        bundle.putSerializable("mapChoosePoiType", mapChoosePoiType);
        navChoosePointInMapFragment.setArguments(bundle);
        return navChoosePointInMapFragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapview.onDestroy();
    }
}
